import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_furnace/lib_furnace.dart';

export 'package:lib_furnace/gt_page_route.dart';
export 'package:lib_furnace/navigator/gt_navigator_observer.dart';

/// Author: Shper
/// Created: 2023-08-10
/// GTRouter
class GTRouter {
  static final GTFurnaceHostApi _hostApi = GTFurnaceHostApi();

  /// 在启动的页面列表
  static final Set<String> pageStartedList = <String>{};

  // 注意: 目前返回值只支持 newContainer = false 的场景.
  static Future<T?> push<T extends Object?>(BuildContext context, String routeName,
      {Map<String, dynamic>? arguments,
        bool newContainer = false,
        bool isDialog = false,
        bool noAnimation = false,
        bool dialogEnableLandscape = false}) {
    // 需要新容器时，转发到原生层
    if (newContainer) {
      _hostApi.openNewContainer(
        routeName,
        arguments,
        isDialog,
        noAnimation: noAnimation,
        dialogEnableLandscape: dialogEnableLandscape,
      );
      return Future.value(null);
    } else {
      return Navigator.pushNamed(context, routeName, arguments: arguments);
    }
  }

  static pop<T extends Object?>(BuildContext context, [T? result]) {
    if (Navigator.of(context).canPop()) {
      Navigator.of(context).pop(result);
    } else {
      // TODO: 支持 result
      SystemNavigator.pop(animated: true);
    }
  }

  static Map<String, dynamic>? findArguments(BuildContext context) {
    var originArgs = ModalRoute
        .of(context)
        ?.settings
        .arguments;
    if (originArgs == null) {
      return null;
    }
    GTLogger.debug("GTRouter findArguments: argumentsType: ${originArgs.runtimeType}");
    if (originArgs is String) {
      return jsonDecode(originArgs);
    }

    if (originArgs is Map<String, dynamic>) {
      return originArgs;
    }

    if (originArgs is Map<String?, Object?>) {
      return Map<String, dynamic>.from(originArgs);
    }

    return null;
  }

  static dynamic findDynamicArguments(BuildContext context) {
    return ModalRoute
        .of(context)
        ?.settings
        .arguments;
  }

  /// 检查某个路由是否正在启动
  static bool isPageStarted(String routeName) {
    return pageStartedList.contains(routeName);
  }

  /// 启动一个页面，
  /// 判断如果这个页面是否已经启动：
  ///   如果已经启动，则关闭这个页面之上的所有页面（包括这个页面）再重新启动这个页面；
  ///   如果没有启动，直接启动这个页面
  static void pushUntilReplacementNamed(BuildContext context, String routeName, {Map<String, dynamic>? arguments,
    bool newContainer = false,
    bool isDialog = false,
    bool noAnimation = false,
    bool dialogEnableLandscape = false}) {
    if (isPageStarted(routeName)) {
      Navigator.popUntil(context, (route) => route.settings.name == routeName);
      Navigator.pushReplacementNamed(context, routeName,arguments: arguments);
    } else {
      GTRouter.push(context, routeName, arguments: arguments,
          newContainer: newContainer,
          isDialog: isDialog,
          noAnimation: noAnimation,
          dialogEnableLandscape: dialogEnableLandscape);
    }
  }

}
